Imports CapaDeNegocios

Partial Public Class FrmAlerta
    Inherits System.Web.UI.Page
    Dim Alertas As nAlerta = New nAlerta
    Dim FIDesde As String
    Dim FIHasta As String
    Dim FInicio As String
    Dim FRecordatorio As String
    Dim OOrigen As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Session("Login") = "Anonimo" Then
                Response.Redirect("/")
            End If
            Inicio()

            ControlarVencimientos()
            Cargargrid()
            CargargridVencidas()
            ' Me.LnkImprimirAlertas.OnClientClick = "javascript:var popup = window.open('../Reportes/RptImprimirAlertas.aspx',(new Date().getTime()),'Width=800,Height=600,Scrollbars=YES,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
            LnkImprimirAlertas.OnClientClick = "javascript:var popup = window.open('../Reportes/RptImprimirAlertas.aspx',(new Date().getTime()),'Width=900,Height=600,Fullscreen= NO,Scrollbars=YES,Resizable=yes,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
        End If
    End Sub
    Sub Fechas()

        'Me.DPFechaInicio.SelectedValue = DBNull.Value
        Me.DPFechaInicioDesde.SelectedValue = DBNull.Value
        Me.DPFechaInicioHasta.SelectedValue = DBNull.Value
        Me.DPFechaRecordatorio.SelectedValue = DBNull.Value
        DPFechaInicio.SelectedDate = Date.Today
        DPFechaInicio.MinimumDate = Date.Today
        DPFechaInicio.Columns = 1




        'DPFechaRecordatorio.SelectedDate = Date.Today
        'DPFechaRecordatorio.MinimumDate = Date.Today
        'DPFechaRecordatorio.Columns = 1
        'Filtros-------------------------------------------------------


        'DPFechaInicioDesde.SelectedDate = Date.Today
        'DPFechaInicioDesde.MinimumDate = Date.Today.
        DPFechaInicioDesde.Columns = 2
        DPFechaInicioDesde.Visible = False

        'DPFechaInicioHasta.SelectedDate = Date.Today.AddDays(7)
        'DPFechaInicioHasta.MinimumDate = Date.Today
        DPFechaInicioHasta.Columns = 2
        DPFechaInicioHasta.Visible = False
    End Sub
    Sub ControlarVencimientos()
        'Alertas.BuscarFiltrado()
        'DEtallegas
        If Alertas.CambiaaVencimiento(Date.Now.ToShortDateString) = True Then
            Me.Response.Write("<script languaje='javascript'>window.alert('Se Encontraron Elementos con Vencimiento el Dia de hoy')</script>")
        End If
    End Sub
    Sub Cargargrid()
        FIDesde = DPFechaInicioDesde.SelectedDateFormatted
        FIHasta = DPFechaInicioHasta.SelectedDateFormatted

        GridAlerta.DataSource = Alertas.BuscarFiltrado("", "", "", "", FIDesde, FIHasta, "", "OK", "", "")
        GridAlerta.DataBind()
        Session("RptAletas") = GridAlerta.DataSource

    End Sub
    Sub CargargridVencidas()
        FIDesde = DPFechaInicioDesde.SelectedDateFormatted
        FIHasta = DPFechaInicioHasta.SelectedDateFormatted

        GridAlertasVencidas.DataSource = Alertas.BuscarFiltrado("", "", "", "", "", "", "", "VENCIDO", "", "")
        GridAlertasVencidas.DataBind()
        Session("RptAletasVencidas") = GridAlertasVencidas.DataSource
    End Sub
    Sub Inicio()
        'Me.TxtFechaInicio.Text = ""
        'Me.TxtFechaVencimiento.Text = ""

        CboOrigen.Visible = True

        Me.DPFechaRecordatorio.Visible = False
        Me.LblRecordatorio.Visible = False
        Me.TxtReferencia.Text = ""
        Me.TxtOrigen.Text = ""
        Session("CodigoAlerta") = ""

        Me.LblFechaInicioDesde.Visible = False
        Me.LblFechaInicioHasta.Visible = False

        Me.LnkBuscar.Visible = True
        Me.LnkBuscar.Enabled = True

        Me.LnkCancelar.Visible = False
        Me.LnkEliminar.Visible = False

        Me.LnkGuardar.Visible = False
        Me.LnkGuardar.Enabled = True

        Me.TxtOrigen.Visible = False
        Me.TxtOrigen.Text = String.Empty

        Me.CboOrigen.SelectedValue = -1

        Me.LblError.Visible = False
        Me.ChkFiltros.Checked = False

        Fechas()

    End Sub
    Sub Buscar()
        FInicio = DPFechaInicio.SelectedDateFormatted
        FRecordatorio = FInicio
        FIDesde = DPFechaInicioDesde.SelectedDateFormatted
        FIHasta = DPFechaInicioHasta.SelectedDateFormatted


        GridAlerta.DataSource = Alertas.BuscarFiltrado("", LTrim(TxtOrigen.Text), LTrim(TxtReferencia.Text), "", _
       FIDesde, FIHasta, "", "OK", "", "")
        GridAlerta.DataBind()


        GridAlertasVencidas.DataSource = Alertas.BuscarFiltrado("", LTrim(TxtOrigen.Text), LTrim(TxtReferencia.Text), "", _
        FIDesde, FIHasta, "", "VENCIDO", "", "")
        GridAlertasVencidas.DataBind()

        Session("RptAletasVencidas") = GridAlertasVencidas.DataSource
        Session("RptAletas") = GridAlerta.DataSource
    End Sub
    Protected Sub ChkFiltros_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkFiltros.CheckedChanged
        If ChkFiltros.Checked = True Then
            Me.LblFechaInicioDesde.Visible = True
            Me.LblFechaInicioHasta.Visible = True
            DPFechaInicioDesde.Visible = True
            DPFechaInicioHasta.Visible = True
            Me.LnkCancelar.Visible = True
        Else
            Me.LblFechaInicioDesde.Visible = False
            Me.LblFechaInicioHasta.Visible = False
            DPFechaInicioDesde.Visible = False
            DPFechaInicioHasta.Visible = False
            DPFechaInicioDesde.SelectedValue = DBNull.Value
            DPFechaInicioHasta.SelectedValue = DBNull.Value
            Me.LnkCancelar.Visible = False
        End If
    End Sub
    Private Sub GridAlerta_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridAlerta.PageIndexChanging
        Me.DPFechaRecordatorio.SelectedValue = DBNull.Value
        GridAlerta.PageIndex = e.NewPageIndex
        Buscar()
    End Sub
    Private Sub GridAlerta_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridAlerta.RowCommand
        Session("CodigoAlerta") = ""

        Select Case e.CommandName
            Case Is = "Select"
                CboOrigen.Visible = False
                Me.DPFechaRecordatorio.SelectedValue = DBNull.Value
                Session("CodigoAlerta") = e.CommandSource.Parent.FindControl("HFCodigoAlerta").value
                'Me.TxtFechaInicio.Text = e.CommandSource.parent.parent.FindControl("LblInicio").text
                Me.DPFechaInicio.SelectedDate = e.CommandSource.parent.parent.FindControl("LblInicio").text
                Me.DPFechaRecordatorio.SelectedDate = e.CommandSource.parent.parent.FindControl("LblVence").text
                Me.DPFechaRecordatorio.Visible = True
                'Me.TxtFechaVencimiento.Text = e.CommandSource.parent.parent.FindControl("LblVence").text
                Me.TxtReferencia.Text = e.CommandSource.parent.parent.FindControl("LblReferencia").text
                Me.TxtOrigen.Text = e.CommandSource.parent.parent.FindControl("LblOrigen").text
                Me.TxtOrigen.Visible = True
                'Me.LnkBuscar.Visible = False
                LnkBuscar.Enabled = False
                'Me.ChkFiltros.Visible = False
                Me.ChkFiltros.Enabled = False
                Me.LnkCancelar.Visible = False
                Me.LnkEliminar.Visible = True
                Me.LnkGuardar.Visible = True

                'Me.TxtFechaInicio.ReadOnly = True
                'Me.TxtFechaVencimiento.ReadOnly = True
                Me.TxtReferencia.ReadOnly = False
                Me.TxtOrigen.ReadOnly = True
                'Me.TxtFechaVencimiento.ReadOnly = True

                Me.LnkGuardar.Visible = True
                Me.LnkCancelar.Visible = True
                Me.LblRecordatorio.Visible = True

            Case Is = "Completo"
                Session("CodigoAlerta") = e.CommandSource.Parent.FindControl("HFCodigoAlerta").value
                Alertas.ModificarEstado(Session("CodigoAlerta"), "COMPLETO")
                Cargargrid()
        End Select
    End Sub
    Protected Sub LnkGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LnkGuardar.Click
        LblError.Text = "Verifique :"
        LblError.Visible = False
        FInicio = DPFechaInicio.SelectedDateFormatted
        FRecordatorio = DPFechaRecordatorio.SelectedDateFormatted
        OOrigen = TxtOrigen.Text


        If CboOrigen.SelectedValue = 100 Then
            If Me.TxtOrigen.Text = "" Then
                LblError.Visible = True
                LblError.Text = LblError.Text & " Origen"
                TxtOrigen.Focus()
                Exit Sub
            End If
            OOrigen = "OTRO" & " :" & TxtOrigen.Text.ToUpper
        End If

        If Me.TxtReferencia.Text = "" Then
            LblError.Visible = True
            LblError.Text = LblError.Text & " Referencia"
            TxtReferencia.Focus()
            Exit Sub
        End If

        'If Me.TxtFechaInicio.Text = "" Then
        '    LblError.Visible = False
        '    LblError.Text = LblError.Text & " Fecha de Incio"
        '    TxtFechaInicio.Focus()
        '    Exit Sub
        'End If
        'If Me.TxtFechaVencimiento.Text = "" Then
        '    LblError.Visible = False
        '    LblError.Text = LblError.Text & " Fecha de Vencimiento"
        '    TxtFechaVencimiento.Focus()
        '    Exit Sub
        'End If

        If Session("CodigoAlerta") = "" Then
            Alertas.Insertar(OOrigen, TxtReferencia.Text, FInicio, FInicio, "", "", "")
        Else
            If Alertas.Modificar(Session("CodigoAlerta"), OOrigen, TxtReferencia.Text, FInicio, _
             FRecordatorio, "", "", "") Then
                'LnkGuardar.Visible = False
                'Inicio()
                'Cargargrid()
            End If
        End If
        'verificando.....
        LnkGuardar.Visible = False
        Inicio()
        Cargargrid()

    End Sub

    Private Sub GridAlertasVencidas_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridAlertasVencidas.PageIndexChanging
        Me.DPFechaRecordatorio.SelectedValue = DBNull.Value
        Me.GridAlertasVencidas.PageIndex = e.NewPageIndex
        Buscar()
    End Sub
    Private Sub GridAlertasVencidas_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridAlertasVencidas.RowCommand
        'Session("CodigoAlerta") = ""
        If e.CommandName = "Eliminar" Then
            Session("CodigoAlerta") = e.CommandSource.Parent.FindControl("HFCodigoAlerta").value
            Alertas.Eliminar(Session("CodigoAlerta"))
            LnkBuscar.Enabled = True
            'CargargridVencidas()
        Else
            If e.CommandName = "Completo" Then
                Session("CodigoAlerta") = e.CommandSource.Parent.FindControl("HFCodigoAlerta").value
                Alertas.ModificarEstado(Session("CodigoAlerta"), "COMPLETO")
                LnkBuscar.Enabled = True
                'CargargridVencidas()
            End If
        End If
        CargargridVencidas()
    End Sub
    Protected Sub LnkBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LnkBuscar.Click
        Buscar()
    End Sub
    Protected Sub LnkCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LnkCancelar.Click
        Inicio()
        LnkBuscar.Enabled = True
        Me.ChkFiltros.Enabled = True
    End Sub
    Protected Sub CboOrigen_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboOrigen.SelectedIndexChanged
        If CboOrigen.SelectedValue = -1 Then
            LnkGuardar.Visible = False
            TxtOrigen.Visible = False
            Exit Sub
        End If
        If CboOrigen.SelectedValue > 0 Then
            LnkGuardar.Visible = True
        End If
        If CboOrigen.SelectedValue = 100 Then
            TxtOrigen.Visible = True
        End If

    End Sub

    Protected Sub GridAlertasVencidas_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridAlertasVencidas.SelectedIndexChanged

    End Sub

    Protected Sub LnkImprimirAlertas_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles LnkImprimirAlertas.Click

    End Sub
End Class